﻿
Public Class Cls_Controles
    Inherits System.Web.UI.UserControl

    Sub CargaControl(ByVal ControlTxt As String, ByVal remove As Boolean, ByRef PlaceHolder1 As PlaceHolder)

        Try
            If remove Then
                RemoveControls(PlaceHolder1)
                'Try
                '    Dim LastFeaturedProductUserControl As Control
                '    LastFeaturedProductUserControl = LoadControl(ViewState("usercontrol"))
                '    Panel2.Controls.Remove(LastFeaturedProductUserControl)

                '    Dim i As Integer
                '    For i = 0 To Panel2.Controls.Count - 1
                '        Panel2.Controls.Remove(Panel2.Controls(i))
                '        Panel2.UpdateAfterCallBack = True
                '    Next

                'Catch ex As Exception

                'End Try
            End If

            Dim FeaturedProductUserControl As Control
            Dim ctl As New System.Web.UI.Control

            FeaturedProductUserControl = LoadControl(ControlTxt)

            Controls.Add(FeaturedProductUserControl)
            PlaceHolder1.Controls.Add(FeaturedProductUserControl)

            ViewState("usercontrol") = ControlTxt

            AddControls(ControlTxt)

            ' PlaceHolder1.UpdateAfterCallBack = True
        Catch ex As Exception

            ViewState("usercontrol") = ""
        End Try

    End Sub
    Sub AddControls(ByVal control As String)

        If ViewState("controles") Is Nothing Then
            ViewState("controles") = control
        Else
            ViewState("controles") = ViewState("controles") & ";" & control
        End If


    End Sub
    Sub RemoveControls(ByRef PlaceHolder1 As PlaceHolder)
        Try
            Dim i As Integer
            Dim j As Integer = PlaceHolder1.Controls.Count - 1

            For i = 0 To PlaceHolder1.Controls.Count - 1
                PlaceHolder1.Controls.Remove(PlaceHolder1.Controls(j))
                'PlaceHolder1.UpdateAfterCallBack = True
                j = j - 1
            Next

            For i = 0 To ViewState("controles").ToString.Split(";").Length - 1

                Try
                    Dim LastFeaturedProductUserControl As Control
                    LastFeaturedProductUserControl = LoadControl(ViewState("controles").ToString.Split(";")(i))
                    PlaceHolder1.Controls.Remove(LastFeaturedProductUserControl)
                    'PlaceHolder1.UpdateAfterCallBack = True

                Catch ex As Exception

                End Try


            Next

            ViewState("controles") = Nothing
        Catch ex As Exception

        End Try
    End Sub

End Class
